package com.xb.hehe.app.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xb.hehe.app.dto.TLoanPlatformDTO;
import com.xb.hehe.app.mapper.TLoanPlatformMapper;
import com.xb.hehe.app.model.TLoanPlatform;
import com.xb.hehe.app.service.TLoanPlatformService;
import com.xb.loan.util.exception.ServiceDataException;
import com.xb.loan.util.page.Page;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * 贷款平台业务层访问接口实现
 * @author  wuql
 * @version tLoanPlatformServiceImpl.java, v 0.1  2017-11-6 15:10:01
 */
@Service("tLoanPlatformService")
public class TLoanPlatformServiceImpl implements TLoanPlatformService {

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TLoanPlatformServiceImpl.class);
    /**贷款平台数据层访问接口实现**/
	@Autowired
	private TLoanPlatformMapper tLoanPlatformMapper;

    @Override
       public Long saveTLoanPlatform(TLoanPlatformDTO tLoanPlatformDTO) {
           logger.info("saveTLoanPlatform tLoanPlatformDTO:{}", tLoanPlatformDTO);
                  long rows = 0;
                  try {
                      if (tLoanPlatformDTO != null) {
                          TLoanPlatform tLoanPlatform = new TLoanPlatform();
                          BeanUtils.copyProperties(tLoanPlatformDTO, tLoanPlatform);
                          //插入
                          tLoanPlatform.setCreateTime(System.currentTimeMillis());
                          rows = tLoanPlatformMapper.insert(tLoanPlatform);
                      }
                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                      throw new ServiceDataException("saveTLoanPlatform Error::", e);
                  }
                  return rows;
       }

    @Override
    public Long updateTLoanPlatform(TLoanPlatformDTO tLoanPlatformDTO) {
        logger.info("saveTLoanPlatform tLoanPlatformDTO:{}", tLoanPlatformDTO);
        long rows = 0;
        try {
            if (tLoanPlatformDTO != null) {
                TLoanPlatform tLoanPlatform = new TLoanPlatform();
                BeanUtils.copyProperties(tLoanPlatformDTO, tLoanPlatform);
                //更新
                rows = tLoanPlatformMapper.updateByPrimaryKeySelective(tLoanPlatform);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceDataException("saveTLoanPlatform Error::", e);
        }
        return rows;
    }

       @Override
       public Long deleteTLoanPlatform(String id) {
       logger.info("deleteTLoanPlatform tLoanPlatformId:{}", id);
           long rows = 0;
           try {
               rows = tLoanPlatformMapper.deleteByPrimaryKey(id);
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("deletetLoanPlatform Error::", e);
           }
           return rows;
       }

       @Override
       public TLoanPlatformDTO queryTLoanPlatformById(String id) {
       logger.info("queryTLoanPlatformById tLoanPlatformId:{}", id);
           TLoanPlatformDTO tLoanPlatformDTO = null;
           try {
              TLoanPlatform tLoanPlatform = tLoanPlatformMapper.selectByPrimaryKey(id);
                           if(tLoanPlatform!=null){
                           tLoanPlatformDTO = new TLoanPlatformDTO();
                               BeanUtils.copyProperties(tLoanPlatform,tLoanPlatformDTO);
                           }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("querytLoanPlatformById", e);
           }
           return tLoanPlatformDTO;
       }

       @Override
       public List<TLoanPlatformDTO> queryTLoanPlatform(TLoanPlatformDTO tLoanPlatformDTO) {
            logger.info("queryTLoanPlatform tLoanPlatformDTO:{}", tLoanPlatformDTO);
                  List<TLoanPlatformDTO> list = null;
                  List<TLoanPlatform> tLoanPlatformList = null;
                  try {
                      if (tLoanPlatformDTO != null) {
                          TLoanPlatform tLoanPlatform = new TLoanPlatform();
                          BeanUtils.copyProperties(tLoanPlatformDTO, tLoanPlatform);
                          tLoanPlatformList = tLoanPlatformMapper.select(tLoanPlatform);
                      } else {
                          Example example = new Example(TLoanPlatform.class);
                          // 排序规则
                          example.orderBy("id").desc();
                          tLoanPlatformList = tLoanPlatformMapper.selectByExample(example);
                      }
                      if (tLoanPlatformList != null && tLoanPlatformList.size() > 0) {
                          list = convertTLoanPlatformDTO(tLoanPlatformList);
                      }

                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                  }
                  return list;
       }

       @Override
       public Page<TLoanPlatformDTO> queryTLoanPlatformPage(TLoanPlatformDTO tLoanPlatformDTO) {
        logger.info("queryTLoanPlatformPage tLoanPlatformDTO:{}", tLoanPlatformDTO);
           Page<TLoanPlatformDTO> page = null;
           Integer currentPage = tLoanPlatformDTO.getPage();
           if (currentPage < 1) currentPage = 1;
           try {
               page = new Page<TLoanPlatformDTO>(currentPage, tLoanPlatformDTO.getPageCount());
               PageHelper.startPage(currentPage, tLoanPlatformDTO.getPageCount());
               Example example = new Example(TLoanPlatform.class);
               // 排序规则
               example.orderBy("id").desc();
               List<TLoanPlatform> list = tLoanPlatformMapper.selectByExample(example);

               if (list != null && list.size() > 0) {
                   PageInfo pageInfo = new PageInfo(list);
                   page.setTotalCount(Long.valueOf(pageInfo.getTotal()).intValue());
                    List<TLoanPlatformDTO> tLoanPlatformDTOList = convertTLoanPlatformDTO(list);
                   page.setResult(tLoanPlatformDTOList);
               }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
           }

           return page;
       }
	  private List<TLoanPlatformDTO> convertTLoanPlatformDTO(List<TLoanPlatform> tLoanPlatformList) {
            
            if (tLoanPlatformList == null) return null;
             List<TLoanPlatformDTO> tLoanPlatformDTOs = new ArrayList<>();
             for (TLoanPlatform tLoanPlatform : tLoanPlatformList
                     ) {
                 TLoanPlatformDTO tLoanPlatformDTO = new TLoanPlatformDTO();
                 BeanUtils.copyProperties(tLoanPlatform, tLoanPlatformDTO);
                 tLoanPlatformDTOs.add(tLoanPlatformDTO);
             }
             return tLoanPlatformDTOs;
         }
}